﻿
namespace GMap.NET.WindowsForms.ToolTips
{
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System;
    using System.Runtime.Serialization;

#if !PocketPC
    /// <summary>
    /// GMap.NET marker
    /// </summary>
    [Serializable]
    public class GMapBaloonToolTip : GMapToolTip, ISerializable
    {
        public float Radius = 10f;

        public static readonly Pen DefaultStroke = new Pen(Color.FromArgb(140, Color.Navy));

        static GMapBaloonToolTip()
        {
            DefaultStroke.Width = 3;

#if !PocketPC
            DefaultStroke.LineJoin = LineJoin.Round;
            DefaultStroke.StartCap = LineCap.RoundAnchor;
#endif
        }

        public GMapBaloonToolTip(GMapMarker marker)
           : base(marker)
        {
            Stroke = DefaultStroke;
            Fill = Brushes.Yellow;
        }

        public override void OnRender(Graphics g)
        {
            System.Drawing.Size st = g.MeasureString(Marker.ToolTipText, Font).ToSize();
            var Rectangle = new System.Drawing.Rectangle(Marker.ToolTipPosition.X, Marker.ToolTipPosition.Y - st.Height, st.Width + TextPadding.Width, st.Height + TextPadding.Height);
           
            Rectangle.Offset(Offset.X, Offset.Y);

            using (GraphicsPath objGP = new GraphicsPath())
            {
                objGP.AddLine(Rectangle.X + 2 * Radius, Rectangle.Y + Rectangle.Height, Rectangle.X + Radius, Rectangle.Y + Rectangle.Height + Radius);
                objGP.AddLine(Rectangle.X + Radius, Rectangle.Y + Rectangle.Height + Radius, Rectangle.X + Radius, Rectangle.Y + Rectangle.Height);

                objGP.AddArc(Rectangle.X, Rectangle.Y + Rectangle.Height - (Radius * 2), Radius * 2, Radius * 2, 90, 90);
                objGP.AddLine(Rectangle.X, Rectangle.Y + Rectangle.Height - (Radius * 2), Rectangle.X, Rectangle.Y + Radius);
                objGP.AddArc(Rectangle.X, Rectangle.Y, Radius * 2, Radius * 2, 180, 90);
                objGP.AddLine(Rectangle.X + Radius, Rectangle.Y, Rectangle.X + Rectangle.Width - (Radius * 2), Rectangle.Y);
                objGP.AddArc(Rectangle.X + Rectangle.Width - (Radius * 2), Rectangle.Y, Radius * 2, Radius * 2, 270, 90);
                objGP.AddLine(Rectangle.X + Rectangle.Width, Rectangle.Y + Radius, Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height - (Radius * 2));
                objGP.AddArc(Rectangle.X + Rectangle.Width - (Radius * 2), Rectangle.Y + Rectangle.Height - (Radius * 2), Radius * 2, Radius * 2, 0, 90); // Corner

                objGP.CloseFigure();

                g.FillPath(Fill, objGP);
                g.DrawPath(Stroke, objGP);
            }

#if !PocketPC
            g.DrawString(Marker.ToolTipText, Font, Foreground, Rectangle, Format);
#else
         g.DrawString(ToolTipText, ToolTipFont, TooltipForeground, Rectangle, ToolTipFormat);
#endif
        }

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Radius", this.Radius);

            base.GetObjectData(info, context);
        }

        protected GMapBaloonToolTip(SerializationInfo info, StreamingContext context)
           : base(info, context)
        {
            this.Radius = Extensions.GetStruct<float>(info, "Radius", 10f);
        }

        #endregion
    }
#endif
}
